﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rabbit.lnfarnum
{
    public class BaseRepository<T>:IBaseRepository<T> where T : class,new()
    {

        private readonly toDBCountext db;

        public BaseRepository(toDBCountext db)
        {
            this.db = db;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> Add(T entity)
        {
            await db.Set<T>().AddAsync(entity);
            return await db.SaveChangesAsync(); 
        }


        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> GetAll()
        {
            return db.Set<T>().AsQueryable();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Task<int> Update(T entity)
        {
           db.Set<T>().Update(entity);
            return db.SaveChangesAsync();
        }


        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entitvs"></param>
        /// <returns></returns>
        public async Task<int> AddRanges(List<T> entitvs)
        {
            db.Set<T>().RemoveRange(entitvs);
            return await db.SaveChangesAsync();
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="entitvs"></param>
        /// <returns></returns>
        public Task<int> DeleteRemove(List<T> entitvs)
        {
            db.Set<T>().RemoveRange(entitvs);
            return  db.SaveChangesAsync();
        }
    }
}
